package interfaces

import (
	"api-gateway-center/config"
	service2 "api-gateway-center/domain/manage/service"
	"api-gateway-center/domain/register/model"
	"api-gateway-center/domain/register/service"
	"api-gateway-center/infrastructure/repository"
	ResponseCode "api-gateway-center/types/enum"
	Response "api-gateway-center/types/model"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

type RpcRegisterManageController struct {
	service       *service.RegisterManageService
	managerServer *service2.ConfigManageService
}

func NewRpcRegisterManageController() *RpcRegisterManageController {
	registerRepository := repository.NewRegisterManageRepository(config.DB)
	registerService := service.NewRegisterManageService(registerRepository)
	manageRepository := repository.NewConfigManageRepository(config.DB)
	manageService := service2.NewConfigManageService(manageRepository)
	return &RpcRegisterManageController{service: registerService, managerServer: manageService}
}
func (this *RpcRegisterManageController) RpcRegisterManage(router *gin.Engine) {
	register := router.Group("/wg/admin/register")
	{
		register.POST("/registerApplication", func(context *gin.Context) {
			data, _ := context.GetRawData()
			var application model.ApplicationSystemVO
			err := json.Unmarshal(data, &application)
			if err != nil {
				context.JSON(http.StatusOK, Response.NewResponse(ResponseCode.UN_ERROR.Code, ResponseCode.UN_ERROR.Info, false))
				return
			}
			err = this.service.RegisterApplication(application)
			if err != nil {
				context.JSON(http.StatusOK, Response.NewResponse(ResponseCode.INDEX_DUP.Code, ResponseCode.INDEX_DUP.Info, false))
				return
			}
			context.JSON(http.StatusOK, Response.NewResponse(ResponseCode.SUCCESS.Code, ResponseCode.SUCCESS.Info, true))
		})
		register.POST("/registerApplicationInterface", func(context *gin.Context) {
			data, _ := context.GetRawData()
			var applicationInterface model.ApplicationInterfaceVO
			err := json.Unmarshal(data, &applicationInterface)
			if err != nil {
				context.JSON(http.StatusOK, Response.NewResponse(ResponseCode.UN_ERROR.Code, ResponseCode.UN_ERROR.Info, false))
				return
			}
			err = this.service.RegisterApplicationInterfacer(applicationInterface)
			if err != nil {
				context.JSON(http.StatusOK, Response.NewResponse(ResponseCode.INDEX_DUP.Code, ResponseCode.INDEX_DUP.Info, false))
				return
			}
			context.JSON(http.StatusOK, Response.NewResponse(ResponseCode.SUCCESS.Code, ResponseCode.SUCCESS.Info, true))
		})
		register.POST("/registerApplicationInterfaceMethod", func(context *gin.Context) {
			data, _ := context.GetRawData()
			var applicationInterfaceMethod model.ApplicationInterfaceMethodVO
			err := json.Unmarshal(data, &applicationInterfaceMethod)
			if err != nil {
				context.JSON(http.StatusOK, Response.NewResponse(ResponseCode.UN_ERROR.Code, ResponseCode.UN_ERROR.Info, false))
				return
			}
			err = this.service.RegisterApplicationInterfaceMethod(applicationInterfaceMethod)
			if err != nil {
				context.JSON(http.StatusOK, Response.NewResponse(ResponseCode.INDEX_DUP.Code, ResponseCode.INDEX_DUP.Info, false))
				return
			}
			context.JSON(http.StatusOK, Response.NewResponse(ResponseCode.SUCCESS.Code, ResponseCode.SUCCESS.Info, true))
		})
		register.POST("/registerEvent", func(context *gin.Context) {
			data, _ := context.GetRawData()
			var m map[string]string
			json.Unmarshal(data, &m)
			gatewayId, err := this.managerServer.QueryGatewayDistribution(m["systemId"])
			if err != nil || gatewayId == "" {
				context.JSON(http.StatusOK, Response.NewResponse(ResponseCode.UN_ERROR.Code, ResponseCode.UN_ERROR.Info, false))
				return
			}
			err = config.Redis.Publish(context, gatewayId, m["systemId"]).Err()
			if err != nil {
				context.JSON(http.StatusOK, Response.NewResponse(ResponseCode.UN_ERROR.Code, ResponseCode.UN_ERROR.Info, false))
				return
			}
			log.Printf("redis向%s发布信息成功", gatewayId)
			context.JSON(http.StatusOK, Response.NewResponse(ResponseCode.SUCCESS.Code, ResponseCode.SUCCESS.Info, true))
		})
	}
}
